<?php
/**
 * Enter description here ...
 * @author NgoAnhTu
 * @date 2013-10-13
 */
class LanguageBusiness {
	private static $instance = null;
	
	/**
	 * Enter description here ...
	 * @var LanguageDAO
	 */
	private $languageDAO;
		
	/**
	 * LanguageBusiness instance only
	 * @return LanguageBusiness
	 */
	public static function getInstance() {
		if (self::$instance == null)
			self::$instance = new LanguageBusiness();
		return self::$instance;
	}
		
	/**
	 * Constructor
	 */
	public function LanguageBusiness() {
		$this->languageDAO = LanguageDAO::getInstance();
	}
	
	/**
	 * @return Ambigous <array<index,, multitype:Ambigous <Entity, NULL> >
	 */
	public function findAll() {
		return $this->languageDAO->select(DAO::FIRST_PAGE, DAO::MAX_SIZE, '', 'Code');
	}
	
	/**
	 * Enter description here ...
	 * @param array $codeArray
	 */
	public function active($codeArray) {
		$codes = "''";
		if (sizeof($codeArray) > 0) {
			foreach ($codeArray as $code) {
				$codes .= ",'$code'";
			}
		}
		$this->languageDAO->active($codes);
	}
	
	/**
	 * Check exist Langauge 
	 * @param string $code
	 * @param string $name
	 * @return boolean
	 */
	public function isExist($code, $name) {
		return $this->languageDAO->isExist(new Language($code, $name));
	}

	/**
	 * Check exist Langauge 
	 * @param unknown_type $code
	 * @param unknown_type $name
	 * @return boolean
	 */
	public function isExistOther($code, $name) {
		return $this->languageDAO->isExistOther(new Language($code, $name));
	}

	/**
	 * Create new Language
	 * @param string $code
	 * @param string $name
	 * @param Image $image
	 * @return Ambigous <Entity, NULL>
	 */
	public function create($code, $name, $image) {
		$lang = new Language(strtolower($code), $name);
		
		$image = new Image($image);
		if ($image->size > 0) {
			$image->saveAs(Properties::LANG_DIR, $code . '.' .Image::EXT_PNG, 17, true);
			$image->destroy();
		}
		
		return $this->languageDAO->insert($lang);
	}

	/**
	 * Update Language
	 * @param Language $lang
	 * @param Image $image
	 * @return Ambigous <Entity, NULL>
	 */
	public function update($lang, $image) {
		$image = new Image($image);
		if ($image->size > 0) {
			$image->saveAs(Properties::LANG_DIR, $lang->code . '.' .Image::EXT_PNG, 17, true);
			$image->destroy();
		}
		
		return $this->languageDAO->update($lang);
	}
	
	/**
	 * Find by Language code
	 * @param string $code
	 * @return Language
	 */
	public function findByCode($code) {
		return $this->languageDAO->selectById(new Language($code));
	}
	
	/**
	 * Enter description here ...
	 * @param unknown_type $code
	 * @return Ambigous <boolean, resource>
	 */
	public function del($code) {
		return $this->languageDAO->delete(new Language($code));
	} 
	
}
?>